package com.weixing.mall.provider.support.excel;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.collect.Lists;
import com.weixing.mall.provider.mapper.CategoryMapper;
import com.weixing.mall.provider.model.domain.Category;
import com.weixing.mall.provider.model.domain.Goods;
import com.weixing.mall.provider.model.enums.GoodsFromEnum;
import com.weixing.mall.provider.model.vo.CategoryVo;
import com.weixing.mall.provider.service.ICategoryService;
import com.weixing.mall.provider.service.IGoodsService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * 活动商品导入模版读取类
 *
 * @author Small
 */
public class GoodsBasicImportDataListener extends AnalysisEventListener<GoodsBasicImportData> {
    private static final Logger LOGGER = LoggerFactory.getLogger(GoodsBasicImportData.class);
    /**
     * 每隔500条存储数据库，实际使用中可以3000条，然后清理list ，方便内存回收
     */
    private static final int BATCH_COUNT = 500;
    List<GoodsBasicImportData> list = new ArrayList<GoodsBasicImportData>();
    @Autowired
    private IGoodsService goodsService;



    public GoodsBasicImportDataListener() {
    }

    @Autowired
    public GoodsBasicImportDataListener(IGoodsService goodsService) {
        this.goodsService = goodsService;

    }

    @Override
    public void invoke(GoodsBasicImportData data, AnalysisContext context) {
        LOGGER.info("解析到一条数据:{}", JSON.toJSONString(data));
        list.add(data);
        if (list.size() >= BATCH_COUNT) {
            saveData();
            list.clear();
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        saveData();
        LOGGER.info("所有数据解析完成！");
    }

    /**
     * 加上存储数据库
     */
    private void saveData() {
        LOGGER.info("{}条数据，开始存储数据库！", list.size());
        List<Goods>  batchList= Lists.newArrayList();
        for (GoodsBasicImportData data : list) {
            Goods goods = new Goods();
            BeanUtils.copyProperties(data, goods);
            List<CategoryVo> tmp = goodsService.findByName(data.getCatName());
            //批量导入
            goods.setGoodsFrom(GoodsFromEnum.EXCEL.getValue());
            goods.setCatId((tmp != null&&tmp.size()!=0) ? tmp.get(0) != null ? tmp.get(0).getId() : null : null);
            if(goods.getCatId()==null){
                goods.setCatName(null);
            }
            if(data.getIsUp().intValue()==0){
                goods.setStatus(2);
            }else{
                goods.setStatus(3);
            }
            goods.setPrintCount(0);
            goods.setStyleNo(data.getStyleNo());
            String styleName=data.getGoodstyle();
            //TODO 这个地方应该使用CMS接口来进行跳转，后期在进行修复把，作用不大，批量导入
            if(styleName.equals("时尚女装")){
                goods.setGoodsSys("68");
            }else if(styleName.equals("LMF系列")){
                goods.setGoodsSys("69");
            } else if(styleName.equals("优雅系列")){
                goods.setGoodsSys("72");
            }else if(styleName.equals("专柜女装")){
                goods.setGoodsSys("73");
            }else if(styleName.equals("专柜女装A")){
                goods.setGoodsSys("77");
            }else if(styleName.equals("意大利系列")){
                goods.setGoodsSys("78");
            }else {
                goods.setGoodsSys("");
            }
            goods.setExpressType(2);//到付
            try{
             Goods goods1=   goodsService.getOne(new QueryWrapper<Goods>().eq("goods_sn",data.getGoodsSn()));
             if(goods1!=null){
                 //BeanUtils.copyProperties(goods, goods1);

                goods.setGoodsSn(data.getGoodsSn()+ UUID.randomUUID().toString().substring(1,4));
                goods.setStyleNo(goods.getGoodsSn());
                goods.setBak1("货号重复，原货号为："+data.getGoodsSn()+"进行了随机货号生成，生成后的为"+goods.getGoodsSn());
                 //goodsService.updateById(goods1);
                 batchList.add(goods);
                goodsService.save(goods);
             }else{
                 batchList.add(goods);
                goodsService.save(goods);
             }
            }catch (Exception e){
                e.printStackTrace();
            }
           // goodsService.saveBatch(batchList);

        }
        LOGGER.info("存储数据库成功！");
    }

}
